"""
根据xls 生成阿里云布尔类型控制模型脚本
xls 地址：./寄存器、网关json、iotjson标示符使用表.xls
输出：寄存器.json
xls扫描行数范围：3~44
"""

from xlrd import open_workbook
import json

workbook = open_workbook(r'./寄存器、网关json、iotjson标示符使用表.xls')  # 打开xls文件
sheet_name = workbook.sheet_names()  # 打印所有sheet名称，是个列表
sheet = workbook.sheet_by_index(0)  # 根据sheet索引读取sheet中的所有内容
sheet1 = workbook.sheet_by_name('Sheet1')  # 根据sheet名称读取sheet中的所有内容
items = {
    "schema"    : "https://iotx-tsl.oss-ap-southeast-1.aliyuncs.com/schema.json",
    "profile"   : {
        "version"   : "1.0",
        "productKey": "gic7bjXqc9e"
    },
    "properties": [],
    "events"    : [
        {
            "identifier": "post",
            "name"      : "post",
            "type"      : "info",
            "required"  : "true",
            "desc"      : "属性上报",
            "method"    : "thing.event.property.post",
            "outputData": []
        }
    ],
    "services"  : [
        {
            "identifier": "set",
            "name"      : "set",
            "required"  : "true",
            "callType"  : "async",
            "desc"      : "属性设置",
            "method"    : "thing.service.property.set",
            "inputData" : [],
            "outputData": []
        },
        {
            "identifier": "get",
            "name"      : "get",
            "required"  : "true",
            "callType"  : "async",
            "desc"      : "属性获取",
            "method"    : "thing.service.property.get",
            "inputData" : [],
            "outputData": []
        }
    ]
}
# 扫描范围：3~44行
for r in range(3, 48):
    bool_propertie = {
        "identifier": sheet.cell(r, 5).value,
        "name"      : sheet.cell(r, 4).value,
        "accessMode": "r",
        "desc"      : sheet.cell(r, 8).value,
        "required"  : "false",
        "dataType"  : {
            "type" : "bool",
            "specs": {
                "0": sheet.cell(r, 6).value,
                "1": sheet.cell(r, 7).value
            }
        }
    }

    int_propertie = {
        "identifier": sheet.cell(r, 5).value,
        "name"      : sheet.cell(r, 4).value,
        "accessMode": "r",
        "desc"      : sheet.cell(r, 8).value,
        "required"  : "false",
        "dataType"  : {
            "type" : "int",
            "specs": {
                "min" : "0",
                "max" : "99999999",
                "step": "1"
            }
        }
    }

    bool_outputData = {
        "identifier": bool_propertie.get("identifier"),
        "name"      : bool_propertie.get("name"),
        "dataType"  : {
            "type" : "bool",
            "specs": bool_propertie.get("dataType").get("specs")
        }
    }
    # int 输出
    int_outputData = {
        "identifier": int_propertie.get("identifier"),
        "name"      : int_propertie.get("name"),
        "dataType"  : {
            "type" : "int",
            "specs": {
                "min" : "0",
                "max" : "99999999",
                "step": "1"
            }
        }
    }

    if (sheet.cell(r, 9).value == 'bool'):
        # print(propertie)
        items["properties"].append(bool_propertie)
        items["events"][0]["outputData"].append(bool_outputData)
        items["services"][1]["outputData"].append(bool_outputData)
        items["services"][1]["inputData"].append(bool_propertie.get("identifier"))
    else:
        items["properties"].append(int_propertie)
        items["events"][0]["outputData"].append(int_outputData)
        items["services"][1]["outputData"].append(int_outputData)
        items["services"][1]["inputData"].append(int_propertie.get("identifier"))


with open("./009阿里云物理模型.json", "w", encoding="utf-8") as f:
    f.write(json.dumps(items, ensure_ascii=False, indent=2))
